import { TDate } from 'src/common/Date'
import { User } from 'src/user/entities/user.entity'
import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
  Unique
} from 'typeorm'

@Entity('role')
@TDate()
export class Role {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  @Unique('role_name', ['name'])
  name: string

  @Column()
  @Unique('role_code', ['code'])
  code: string

  @Column({ nullable: true })
  description: string

  @ManyToOne(() => User, (user) => user.id, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'created_by' })
  createdBy: number

  @ManyToOne(() => User, (user) => user.id, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'updated_by' })
  updatedBy: number

  constructor(role?: Partial<Role>) {
    Object.assign(this, role)
  }
}
